﻿namespace WMS6.Web.Administration
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using System.Text;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.MessageServiceRef;

    public class SendMessage : BasePage
    {
        protected Button btnDelete;
        protected Button btnGoBack;
        protected Button btnNew;
        protected Button btnSave;
        protected Button btnSend;
        protected HtmlGenericControl con_one_2;
        protected HtmlForm F1227;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnNoUserIDs;
        protected HtmlInputHidden hdnRowID;
        protected HtmlInputHidden hdnYesUserIDs;
        protected HtmlHead Head1;
        protected ImageButton ImgAdd;
        protected ImageButton ImgSub;
        protected Label lblMessageContent;
        protected Label lblMessageInfo;
        protected Label lblMessageTitle;
        protected Label lblMsg;
        protected Label lblNoUserIDs;
        protected Label lblYesUserIDs;
        protected ListBox lbxNoUserIDs;
        protected ListBox lbxYesUserIDs;
        protected HtmlLink mycss;
        protected ScriptManager ScriptManager1;
        protected TextBox txtMessageContent;
        protected TextBox txtMessageTitle;
        protected UpdatePanel up1;

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(this.hdnRowID.Value))
                {
                    List<string> rowIDs = new List<string> {
                        this.hdnRowID.Value
                    };
                    if (int.Parse(MessageServiceClientProxy.GetInstance().DeleteSendMessage(rowIDs)) > 0)
                    {
                        LicenseUtils.IsUnreadMessageChange = true;
                        base.Response.Redirect("SendMessageList.aspx?from=edit", false);
                    }
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F4100_013") + ex.Message;
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("SendMessageQuery.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("SendMessageList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            this.txtMessageTitle.Text = string.Empty;
            this.txtMessageContent.Text = string.Empty;
            this.txtMessageTitle.Enabled = true;
            this.txtMessageContent.Enabled = true;
            this.btnSave.Enabled = false;
            this.btnSend.Enabled = true;
            this.btnDelete.Enabled = false;
            this.hdnRowID.Value = string.Empty;
            this.con_one_2.Style.Add("display", "block");
            this.lbxYesUserIDs.Items.Clear();
            this.getUserLBXList();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(this.hdnRowID.Value))
                {
                    if (string.IsNullOrEmpty(this.txtMessageTitle.Text.Trim()))
                    {
                        this.lblMsg.Text = base.GetGlobalMsgResource("F9230_009");
                    }
                    else if (Encoding.Default.GetByteCount(this.txtMessageContent.Text.Trim()) > 500)
                    {
                        this.lblMsg.Text = base.GetGlobalMsgResource("F9230_011");
                    }
                    else if (((this.txtMessageTitle.Text.Trim().Contains<char>('*') || this.txtMessageContent.Text.Trim().Contains<char>('*')) || this.txtMessageTitle.Text.Trim().Contains<char>('|')) || this.txtMessageContent.Text.Trim().Contains<char>('|'))
                    {
                        this.lblMsg.Text = base.GetGlobalMsgResource("F9230_012");
                    }
                    else
                    {
                        UserMessageInfo userMessageInfo = base.DataEntityViewState as UserMessageInfo;
                        if (userMessageInfo != null)
                        {
                            userMessageInfo.RowID = int.Parse(this.hdnRowID.Value);
                            userMessageInfo.MessageTitle = this.txtMessageTitle.Text.Trim();
                            userMessageInfo.MessageContent = this.txtMessageContent.Text.Trim();
                            LicenseUtils.IsUnreadMessageChange = true;
                            this.lblMsg.Text = MessageServiceClientProxy.GetInstance().UpdateUserMessageInfo(userMessageInfo) + base.GetGlobalMsgResource("F4100_009");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F4100_010") + ex.ToString();
            }
        }

        protected void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(this.txtMessageTitle.Text.Trim()))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F9230_009");
                }
                else if (this.lbxYesUserIDs.Items.Count <= 0)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F9230_010");
                }
                else if (Encoding.Default.GetByteCount(this.txtMessageContent.Text.Trim()) > 500)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F9230_011");
                }
                else if (((this.txtMessageTitle.Text.Trim().Contains<char>('*') || this.txtMessageContent.Text.Trim().Contains<char>('*')) || this.txtMessageTitle.Text.Trim().Contains<char>('|')) || this.txtMessageContent.Text.Trim().Contains<char>('|'))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F9230_012");
                }
                else
                {
                    List<UserMessageInfo> userMessageList = new List<UserMessageInfo>();
                    UserMessageInfo userMessageInfo = null;
                    for (int i = 0; i < this.lbxYesUserIDs.Items.Count; i++)
                    {
                        userMessageInfo = new UserMessageInfo {
                            MessageTitle = this.txtMessageTitle.Text.Trim(),
                            MessageContent = this.txtMessageContent.Text.Trim(),
                            FromUserID = base.UserInfo.UserID,
                            CreatedBy = base.UserInfo.UserID,
                            UpdatedBy = base.UserInfo.UserID,
                            CreatedDate = DateTime.Now,
                            UpdatedDate = DateTime.Now,
                            SystemCreated = "N",
                            IsRead = "N",
                            IsActive = "N",
                            ToUserID = this.lbxYesUserIDs.Items[i].Text
                        };
                        userMessageList.Add(userMessageInfo);
                    }
                    if (int.Parse(MessageServiceClientProxy.GetInstance().InsertUserMessageInfoList(userMessageList)) > 0)
                    {
                        this.lblMsg.Text = base.GetGlobalMsgResource("F9230_013");
                        this.txtMessageTitle.Text = string.Empty;
                        this.txtMessageContent.Text = string.Empty;
                        this.hdnRowID.Value = string.Empty;
                        this.hdnNoUserIDs.Value = string.Empty;
                        this.hdnYesUserIDs.Value = string.Empty;
                        this.lbxYesUserIDs.Items.Clear();
                        this.getUserLBXList();
                        LicenseUtils.IsUnreadMessageChange = true;
                    }
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9230_014") + ex.ToString();
            }
        }

        private void getUserLBXList()
        {
            DataSet ds = BaseInfoServiceClientProxy.GetInstance().GetAllInfo("USER_MST");
            this.lbxNoUserIDs.DataSource = ds;
            this.lbxNoUserIDs.DataTextField = "USER_ID";
            this.lbxNoUserIDs.DataValueField = "USER_ID";
            this.lbxNoUserIDs.DataBind();
        }

        protected void ImgAdd_Click(object sender, ImageClickEventArgs e)
        {
            if (!string.IsNullOrEmpty(this.hdnYesUserIDs.Value))
            {
                string[] strYesUserIDs = this.hdnYesUserIDs.Value.Split(new char[] { ';' });
                for (int i = 0; strYesUserIDs.Length > i; i++)
                {
                    ListItem item = new ListItem {
                        Text = strYesUserIDs[i],
                        Value = strYesUserIDs[i]
                    };
                    this.lbxNoUserIDs.Items.Add(item);
                    for (int j = 0; j < this.lbxYesUserIDs.Items.Count; j++)
                    {
                        if (this.lbxYesUserIDs.Items[j].Value == item.Value)
                        {
                            this.lbxYesUserIDs.Items.Remove(item);
                        }
                    }
                }
                this.hdnYesUserIDs.Value = "";
            }
            else
            {
                this.lblMsg.Visible = true;
                this.lblMsg.Text = base.GetGlobalMsgResource("F1300_019");
            }
        }

        protected void ImgSub_Click(object sender, ImageClickEventArgs e)
        {
            if (!string.IsNullOrEmpty(this.hdnNoUserIDs.Value))
            {
                string[] strNoUserIDs = this.hdnNoUserIDs.Value.Split(new char[] { ';' });
                for (int i = 0; strNoUserIDs.Length > i; i++)
                {
                    ListItem item = new ListItem {
                        Text = strNoUserIDs[i],
                        Value = strNoUserIDs[i]
                    };
                    this.lbxYesUserIDs.Items.Add(item);
                    for (int j = 0; j < this.lbxNoUserIDs.Items.Count; j++)
                    {
                        if (this.lbxNoUserIDs.Items[j].Value == item.Value)
                        {
                            this.lbxNoUserIDs.Items.Remove(item);
                        }
                    }
                }
                this.hdnNoUserIDs.Value = "";
            }
            else
            {
                this.lblMsg.Visible = true;
                this.lblMsg.Text = base.GetGlobalMsgResource("F1300_019");
            }
        }

        protected void lbxNoUserIDs_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.hdnNoUserIDs.Value = "";
            if (this.lbxNoUserIDs.Items.Count > 0)
            {
                foreach (ListItem info in this.lbxNoUserIDs.Items)
                {
                    if (info.Selected)
                    {
                        this.hdnNoUserIDs.Value = this.hdnNoUserIDs.Value + info.Value + ";";
                    }
                }
                if (!string.IsNullOrEmpty(this.hdnNoUserIDs.Value))
                {
                    this.hdnNoUserIDs.Value = this.hdnNoUserIDs.Value.Substring(0, this.hdnNoUserIDs.Value.Length - 1);
                }
            }
        }

        protected void lbxYesUserIDs_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.hdnYesUserIDs.Value = "";
            if (this.lbxYesUserIDs.Items.Count > 0)
            {
                foreach (ListItem info in this.lbxYesUserIDs.Items)
                {
                    if (info.Selected)
                    {
                        this.hdnYesUserIDs.Value = this.hdnYesUserIDs.Value + info.Value + ";";
                    }
                }
                if (!string.IsNullOrEmpty(this.hdnYesUserIDs.Value))
                {
                    this.hdnYesUserIDs.Value = this.hdnYesUserIDs.Value.Substring(0, this.hdnYesUserIDs.Value.Length - 1);
                }
            }
        }

        private void LoadData()
        {
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            this.SetRoleInfo2Form();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            base.Response.Buffer = true;
            base.Response.ExpiresAbsolute = DateTime.Now.AddSeconds(-1.0);
            base.Response.Expires = 0;
            base.Response.CacheControl = "no-cache";
            base.Response.AddHeader("Pragma", "No-Cache");
            this.lblMsg.Text = "";
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetRoleInfo2Form()
        {
            string roleId = base.Request.QueryString["ID"];
            if (!string.IsNullOrEmpty(roleId))
            {
                UserMessageInfo userMessageInfo = MessageServiceClientProxy.GetInstance().GetUserMessageInfoByQueryEntity(roleId);
                if (userMessageInfo != null)
                {
                    base.DataEntityViewState = userMessageInfo;
                    this.txtMessageTitle.Text = userMessageInfo.MessageTitle;
                    this.txtMessageContent.Text = userMessageInfo.MessageContent;
                    this.hdnRowID.Value = roleId;
                    if (userMessageInfo.IsRead == "Y")
                    {
                        this.txtMessageTitle.Enabled = false;
                        this.txtMessageContent.Enabled = false;
                        this.btnSave.Enabled = false;
                        this.btnSend.Enabled = false;
                        this.btnDelete.Enabled = false;
                    }
                    else
                    {
                        this.txtMessageTitle.Enabled = true;
                        this.txtMessageContent.Enabled = true;
                        this.btnSave.Enabled = true;
                        this.btnSend.Enabled = false;
                        this.btnDelete.Enabled = true;
                    }
                }
                this.con_one_2.Style.Add("display", "none");
            }
            else
            {
                this.txtMessageTitle.Enabled = true;
                this.txtMessageContent.Enabled = true;
                this.btnSave.Enabled = false;
                this.btnSend.Enabled = true;
                this.btnDelete.Enabled = false;
                this.con_one_2.Style.Add("display", "block");
                this.getUserLBXList();
            }
        }
    }
}

